package com.xtaller.common.convert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
 * @author Taller
 * @create 2018-04-13 17:18
 */
public class J {
    public static String o2s(Object object){
        return JSONObject.toJSONString(object);
    }
    public static JSONObject o2j(Object object){
        return JSONObject.parseObject(o2s(object));
    }
    public static <T> T o2m(Object object,Class<T> cls){
        return JSONObject.parseObject(o2s(object),cls);
    }
    public static List<JSONObject> o2l(Object object){
        return JSONObject.parseArray(o2s(object),JSONObject.class);
    }
    public static <T> List<T> o2l(Object object,Class<T> cls){
        return JSONObject.parseArray(o2s(object),cls);
    }

    public static JSONObject s2j(String json){
        return JSONObject.parseObject(json);
    }
    public static <T> T s2m(String json,Class<T> cls){
        return JSONObject.parseObject(json,cls);
    }
    public static List<JSONObject> s2l(String json){
        return JSONObject.parseArray(json,JSONObject.class);
    }
    public static <T> List<T> s2l(String json,Class<T> cls){
        return JSONObject.parseArray(json,cls);
    }

    public static Map<String,Object> o2m(Object object){
        return JSON.parseObject(o2s(object), Map.class);
    }
    public static Map<String,Object> s2m(String json){
        return JSON.parseObject(json, Map.class);
    }

    public static String s2r(String message){
        JSONObject object = new JSONObject();
        object.put("message",message);
        object.put("result",new JSONObject());
        object.put("isRefresh", 0);
        return J.o2s(object);
    }
    public static String s2r(String message, int isRefresh){
        JSONObject object = new JSONObject();
        object.put("message",message);
        object.put("result",new JSONObject());
        object.put("isRefresh", isRefresh);
        return J.o2s(object);
    }
    public static String s2r(String message,Object o){
        JSONObject object = new JSONObject();
        object.put("message",message);
        object.put("result",o);
        object.put("isRefresh", 0);
        return J.o2s(object);
    }

    public static JSONObject arr2j(String arr[]){
        JSONObject object = new JSONObject();
        for(String info: arr){
            object.put(info,"");
        }
        return object;
    }

    // 处理obj 去掉null的字段
    public static Object formatObject(Object object){
        Object o = new Object();
        try{
            List<JSONObject> list = J.o2l(object);
            if(list.size() > 0){
                for(JSONObject o1:list){
                    LinkedHashMap<String, String> jsonMap = JSON.parseObject(
                            J.o2s(o1),
                            new TypeReference<LinkedHashMap<String, String>>() {}
                    );
                    for (Map.Entry<String, String> entry : jsonMap.entrySet()) {
                        if(entry.getValue() == null){
                            o1.remove(entry.getKey());
                        }
                    }
                }
            }
            o = list;
        }catch (Exception e){
            JSONObject o2 = J.o2j(object);
            LinkedHashMap<String, String> jsonMap = JSON.parseObject(
                    J.o2s(o2),
                    new TypeReference<LinkedHashMap<String, String>>() {}
            );
            for (Map.Entry<String, String> entry : jsonMap.entrySet()) {
                System.out.println(entry.getKey() + ":" + entry.getValue());
                if(entry.getValue() == null){
                    o2.remove(entry.getKey());
                }
            }
            o = o2;
        }
        return o;
    }
}
